<!-- 年度应付查询：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList2" @findClicked="findClicked2" />
    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="flex AIC">
          <h1 class="f16">结算应付汇总查询/年度</h1>
          <el-radio-group v-model="radio" class="ml10" @change="radioChange">
            <el-radio value="1">大于0</el-radio>
            <el-radio value="0">小于0</el-radio>
          </el-radio-group>
        </div>
        <Table
          :tableHeader="tableHeader"
          :tableData="tableData"
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
          :summaryData="summaryData"
          summary
        />
        <PageModule
          :PageData="{ total, pageSizeShow: true, buttonS: true }"
          :layout="'prev, pager'"
          @pageSizeChange="handleSizeChange"
          @pageChange="handleCurrentChange"
        />
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">结算应付汇总查询/月份</h1>
        <Table
          :tableHeader="tableHeader2"
          :tableData="tableData2"
          class="mt10"
          :isLoading="isLoading2"
          :summaryData="summaryData2"
          summary
          @currentRow1="dbCurrentRow2"
        />
        <h5 class="f12" style="color: #0040c0">双击查看详情</h5>
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">付款明细/月份</h1>
        <Table :tableHeader="tableHeader3" :tableData="tableData3" :isLoading="isLoading3" :summaryData="summaryData3" summary />
        <PageModule
          :PageData="{ total: total3, pageSizeShow: true, buttonS: true }"
          :layout="'prev, pager'"
          @pageSizeChange="handleSizeChange3"
          @pageChange="handleCurrentChange3"
        />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import fileRequest from '@/utils/fileRequest'
import LoginWork from '@/utils/LoginWork'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'AnnualInquiryPayableKl'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import type { formListType } from '@/components/Find/index.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'
const Greater = ref(false)
const Less = ref(false)
const formList = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '结算类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    dateType: 'year',
    type: 'dateMonth',
    dateLabel: '结算年份',
    dateValue: '2025'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '更新对账数据'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  },
  {
    type: 'button',
    checkboxLabel: '导出对账统计表'
  }
]) as any

const formList2 = ref([
  {
    type: 'input',
    inputPlaceholder: '单据号码',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '付款日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '导出'
  }
]) as any

// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '更新对账数据') {
    const res = (await LoginWork.get('/AnnualPayable/updateStatistics')) as any
    if (res.code === 200) {
      JMessage.success('更新成功')
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/AnnualPayable/exportSettlementSummary`, {
      financeType: formList.value.find(
        (item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算类型'
      )?.selectValue,
      vendorName: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
        ?.selectValue.split(' ')[0],
      vendorNo: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
        ?.selectValue.split(' ')[1],
      financeYymm: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '结算年份'
      )?.dateValue,
      judgFlag: radio.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
  if (content === '导出对账统计表') {
    if (formList.value[2].dateValue === '') {
      JMessage.warning('请选择结算年份')
      return
    }
    const res = (await fileRequest.get(`/AnnualPayable/exportReconciliationStatistics?financeYear=${formList.value[2].dateValue}`)) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 3,
    num: 0
  },
  {
    index: 4,
    num: 0
  },
  {
    index: 5,
    num: 0
  },
  {
    index: 6,
    num: 0
  },
  {
    index: 7,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
const radioChange = (val: any) => {
  radio.value = val
  getTableList()
}
const radio = ref('')
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/AnnualPayable/getSettlementSummaryByAnnual', {
    financeType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算类型')
      ?.selectValue,
    vendorName: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectValue.split(' ')[0],
    vendorNo: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectValue.split(' ')[1],
    financeYymm: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '结算年份'
    )?.dateValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value,
    judgFlag: radio.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  currentRow()
  summaryData.value[0].num = tableDataList.amt
  summaryData.value[1].num = tableDataList.unBalanceAmt
  summaryData.value[2].num = tableDataList.balanceAmt
  summaryData.value[3].num = tableDataList.apAmt
  summaryData.value[4].num = tableDataList.unApAmt
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
const tableHeader = ref([
  {
    width: '150',
    label: '厂商',
    prop: 'vendorName'
  },
  {
    width: '60',
    label: '账期',
    prop: 'vendorZq'
  },
  {
    width: '60',
    label: '结算年份',
    prop: 'yy'
  },
  {
    width: '75',
    label: '总金额',
    prop: 'amt'
  },
  {
    width: '75',
    label: '未对账金额',
    prop: 'unBalanceAmt'
  },
  {
    width: '75',
    label: '已对账金额',
    prop: 'balanceAmt'
  },
  {
    width: '75',
    label: '已付金额',
    prop: 'apAmt'
  },
  {
    width: '75',
    label: '未付金额',
    prop: 'unApAmt'
  },
  {
    width: '75',
    label: '到期未付',
    prop: 'unAmtDq'
  },
  {
    width: '75',
    label: '逾期未付',
    prop: 'unAmtYq'
  }
])
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
  getTableList2()
}

const tableHeader2 = ref([
  {
    label: '结算月份',
    prop: 'financeYymm'
  },
  {
    label: '已对账金额',
    prop: 'balanceAmt'
  },
  {
    label: '已付金额',
    prop: 'apAmt'
  },
  {
    label: '未付金额',
    prop: 'unApAmt'
  }
])
// 表二模块
// 表二按钮
const findClicked2 = async (content: string) => {
  if (content === '置空') {
    formList2.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '导出') {
    const res = (await fileRequest.post(`/AnnualPayable/exportPaymentDetails`, {
      slipNo: formList2.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '单据号码'
      )?.inputValue,
      vendorName: formList2.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
        ?.selectValue?.split(' ')[0],
      vendorNo: formList2.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
        ?.selectValue?.split(' ')[1],
      dateStart: formList2.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '付款日期'
      )?.dateValue?.[0],
      dateEnd: formList2.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '付款日期'
      )?.dateValue?.[1],
      ...currentTableRow2.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`utf-8''`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData2 = ref([])
const isLoading2 = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData2 = ref<summaryDataType[]>([
  {
    index: 2,
    num: 0
  },
  {
    index: 3,
    num: 0
  },
  {
    index: 4,
    num: 0
  }
])
// 分页器
const currentPage2 = ref(1)
const pageSize2 = ref(40)
const total2 = ref(0)
const handleSizeChange2 = (val: number) => {
  pageSize2.value = val
  getTableList2()
}
const handleCurrentChange2 = (val: number) => {
  currentPage2.value = val
  getTableList2()
}
// 获取表数据
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.post<any, any>('/AnnualPayable/getSettlementSummaryDetails', currentTableRow.value)
  isLoading2.value = false
  tableData2.value = tableDataList.list
  total2.value = tableDataList.total
  summaryData2.value[0].num = tableDataList.balanceAmt
  summaryData2.value[1].num = tableDataList.apAmt
  summaryData2.value[2].num = tableDataList.unApAmt
}, 500)
// 点击表给另一张表传数据
const currentTableRow2 = ref({}) as any
const dbCurrentRow2 = async (row: any = tableData.value[0]) => {
  currentTableRow2.value = row
  getTableList3()
}

// 表三模块
const tableData3 = ref([])
const isLoading3 = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData3 = ref<summaryDataType[]>([
  {
    index: 3,
    num: 0
  }
])
// 分页器
const currentPage3 = ref(1)
const pageSize3 = ref(40)
const total3 = ref(0)
const handleSizeChange3 = (val: number) => {
  pageSize3.value = val
  getTableList3()
}
const handleCurrentChange3 = (val: number) => {
  currentPage3.value = val
  getTableList3()
}
// 获取表数据
const getTableList3 = debounce(async () => {
  isLoading3.value = true
  let tableDataList = await service.post<any, any>('/AnnualPayable/getPaymentDetailsAndMonthAndAmount', {
    slipNo: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '单据号码'
    )?.inputValue,
    vendorName: formList2.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectValue?.split(' ')[0],
    vendorNo: formList2.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectValue?.split(' ')[1],
    dateStart: formList2.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '付款日期'
    )?.dateValue?.[0],
    dateEnd: formList2.value.find((item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '付款日期')
      ?.dateValue?.[1],
    pageNum: currentPage3.value,
    pageSize: pageSize3.value,
    ...currentTableRow2.value
  })
  isLoading3.value = false
  tableData3.value = tableDataList.list
  total3.value = tableDataList.total
  // currentRow3()
  summaryData3.value[0].num = tableDataList.count
}, 500)
const tableHeader3 = ref([
  {
    width: '120px',
    label: '单据号码',
    prop: 'slipNo'
  },
  {
    label: '厂商名称',
    prop: 'vendorName'
  },
  {
    label: '付款日期',
    prop: 'financeYymm'
  },
  {
    label: '金额',
    prop: 'amt'
  },
  {
    label: '备注',
    prop: 'remark'
  },
  {
    label: '操作员',
    prop: 'sysUser'
  }
])
onMounted(() => {
  getTableList3()
})
watch(
  () =>
    formList2.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList3()
  }
)

const getSelectList = async () => {
  let vendorName = await service.get<any, any>('/back/vendorQuery')
  // 插入厂商代码 厂商名称
  vendorName.forEach((item: any, index: any) => {
    if (item === null) return
    formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectOptions?.push({
        optionKey: `${item.vendorName} ${item.vendorNo}`,
        optionLabel: `${item.vendorName} ${item.vendorNo}`,
        optionValue: `${item.vendorName} ${item.vendorNo}`,
        LeftLabel: `${item.vendorName}`,
        RightLabel: `${item.vendorNo}`
      })
    formList2.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商')
      ?.selectOptions?.push({
        optionKey: `${item.vendorName} ${item.vendorNo}`,
        optionLabel: `${item.vendorName} ${item.vendorNo}`,
        optionValue: `${item.vendorName} ${item.vendorNo}`,
        LeftLabel: `${item.vendorName}`,
        RightLabel: `${item.vendorNo}`
      })
  })
  formList.value
    .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '结算类型')
    ?.selectOptions?.push(
      {
        optionKey: `现金`,
        optionLabel: `现金`,
        optionValue: `现金`
      },
      {
        optionKey: `月结`,
        optionLabel: `月结`,
        optionValue: `月结`
      }
    )
}
onMounted(() => {
  getSelectList()
})
</script>

<style lang="less" scoped></style>
